package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author atguigu-mqx
 */
@Service
public class ItemServiceImpl implements ItemService {

    //  实现类中要调用
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getItem(Long skuId) {
        //  声明一个map 集合！
        Map<String, Object> map = new HashMap<>();
        //  获取skuInfo
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  根据skuId 来获取 skuInfo 对象 ，这个对象中有skuInfo + skuImage 数据！
            SkuInfo skuInfo = this.productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //  获取分类数据：
            BaseCategoryView categoryView = this.productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        },threadPoolExecutor);


        //  获取价格数据：
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = this.productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        },threadPoolExecutor);


        //  获取回显销售属性 + 锁定！
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = this.productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());

            map.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);

        //  获取海报：
        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = this.productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());

            map.put("spuPosterList", spuPosterList);
        }, threadPoolExecutor);

        //  获取到Json 数据！ {"3729|3730":"22","3729|3731":"21"}
        CompletableFuture<Void> valueJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            //  将map 转换成json
            String valueJson = JSON.toJSONString(idsMap);

            map.put("valuesSkuJson", valueJson);
        },threadPoolExecutor);

        //  热度排名
        CompletableFuture<Void> hotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //  获取规则参数：
        CompletableFuture<Void> attrListCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);
            List<HashMap<String, Object>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
                //  声明一个map 集合！
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("attrName", baseAttrInfo.getAttrName());
                hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashMap;
            }).collect(Collectors.toList());
            //  给map 赋值！
            map.put("skuAttrList", skuAttrList);
        },threadPoolExecutor);

        //  查询完成之后，使用多任务组合！
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                attrListCompletableFuture,
                priceCompletableFuture,
                categoryViewCompletableFuture,
                spuPosterListCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valueJsonCompletableFuture,
                hotScoreCompletableFuture
        ).join();
        //  返回数据！
        return map;
    }
}
